﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.WeChatOfficialAccount.DAL.DAL.Common;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.DAL.Model.Business;
using XQ.WeChatOfficialAccount.DAL.Models;

namespace XQ.WeChatOfficialAccount.DAL.DAL.Business
{
    /// <summary>
    /// 类别数据操作类
    /// </summary>
    public class CategoryDal:BaseDal<Category>
    {
        /// <summary>
        /// 根据条件获取列表
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        public List<Category> GetList(CategorySearchModel searchModel, Pagination page, TokenModel token)
        {
            using (WeChatOfficialAccountEntities woae = new WeChatOfficialAccountEntities())
            {
                //分页信息
                StringBuilder sqlPage = new StringBuilder();
                sqlPage.Append(" select count(1) ");
                sqlPage.Append(" from Category c");
                sqlPage.Append(" where 1=1 ");
                //类别ID
                if (searchModel.ParentID>0)
                {
                    sqlPage.Append(" and c.ParentID=" + searchModel.ParentID);
                }
                
                int allCount = woae.Database.SqlQuery<int>(sqlPage.ToString()).First();
                page.RecordCount = allCount;
                page.PageCount = (int)Math.Ceiling((decimal)page.RecordCount / (decimal)searchModel.pageSize);
                page.PageIndex = searchModel.index;
                page.PageSize = searchModel.pageSize;

                //数据列表
                StringBuilder sql = new StringBuilder();
                sql.Append("select top " + searchModel.pageSize + " * from ");
                sql.Append(" (select row_number() over(order by c.sortNum) as rowNum, ");
                sql.Append(" c.*");
                sql.Append(" from Category c ");
                sql.Append(" where 1=1 ");

                //类别ID
                if (searchModel.ParentID > 0)
                {
                    sql.Append(" and c.ParentID=" + searchModel.ParentID);
                }
                
                sql.Append(") t1 ");
                sql.Append("where rowNum>" + (searchModel.index - 1) * searchModel.pageSize);
                LogBll.Instance.WriteLog(LogBll.LogType.Search, "", token.UID, "获取分类列表SQL", "", "", "", sql.ToString());
                return woae.Database.SqlQuery<Category>(sql.ToString()).ToList();
            }
        }

        /// <summary>
        /// 更新类别
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public int UpdateCategory(Category category)
        {
            using (WeChatOfficialAccountEntities wae = new WeChatOfficialAccountEntities())
            {
                Category curCategory = wae.Category.FirstOrDefault(c => c.id == category.id);
                curCategory.ImageUrl = category.ImageUrl;
                curCategory.LinkUrl = category.LinkUrl;
                curCategory.ParentID = category.ParentID;
                curCategory.CategoryName = category.CategoryName;
                curCategory.CategoryType = category.CategoryType;
                curCategory.UpdateTime = category.UpdateTime;
                curCategory.UpdateUserID = category.UpdateUserID;
                curCategory.Level = category.Level;

                return wae.SaveChanges();
            }
        }
    }
}
